﻿import { writeFile } from 'fs/promises';

function uiFileContent(className: string, baseClassName: string): string {
    if (baseClassName === `QMainWindow`) {
        return `<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>${className}</class>
 <widget class="${baseClassName}" name="${className}">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>800</width>
    <height>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>${className}</string>
  </property>
  <widget class="QWidget" name="centralwidget"/>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>800</width>
     <height>25</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>`;
    }
    return `<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>${className}</class>
 <widget class="${baseClassName}" name="${className}">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>300</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>${className}</string>
  </property>
 </widget>
 <resources/>
 <connections/>
</ui>`;
}


function headFileContent(className: string, baseClassName: string): string {
    if (baseClassName === `QMainWindow`) {
        return `#ifndef ${className.toUpperCase()}_H
#define ${className.toUpperCase()}_H

#include <${baseClassName}>

QT_BEGIN_NAMESPACE
namespace Ui {
class ${className};
}
QT_END_NAMESPACE

class ${className} : public ${baseClassName}
{
    Q_OBJECT

public:
    explicit ${className}(QWidget *parent = nullptr);
    ~${className}() override;

private:
    Ui::${className} *ui;
};
#endif // ${className.toUpperCase()}_H`;
    }
    return `#ifndef ${className.toUpperCase()}_H
#define ${className.toUpperCase()}_H

#include <${baseClassName}>

namespace Ui {
class ${className};
}

class ${className} : public ${baseClassName}
{
    Q_OBJECT

public:
    explicit ${className}(QWidget *parent = nullptr);
    ~${className}() override;

private:
    Ui::${className} *ui;
};

#endif // ${className.toUpperCase()}_H`;
}


function sourceFileContent(className: string, baseClassName: string): string {
    
    return `#include "${className.toLowerCase()}.h"
#include "ui_${className.toLowerCase()}.h"

${className}::${className}(QWidget *parent)
    : ${baseClassName}(parent)
    , ui(new Ui::${className})
{
    ui->setupUi(this);
}

${className}::~${className}()
{
    delete ui;
}
`;
    
}

export async function newUiFile(fileName: string, className: string, baseClassName: string): Promise<boolean> {
    try {
        // 调用 writeFile 方法异步写入文件
        await writeFile(fileName, uiFileContent(className, baseClassName), 'utf8');
        return true;
    } catch (error) {
        console.error('异步创建文件时出错:', error);
    }
    return false;
}

export async function newHeadFile(fileName: string, className: string, baseClassName: string): Promise<boolean> {
    try {
        // 调用 writeFile 方法异步写入文件
        await writeFile(fileName, headFileContent(className, baseClassName), 'utf8');
        return true;
    } catch (error) {
        console.error('异步创建文件时出错:', error);
    }
    return false;
}

export async function newSourceFile(fileName: string, className: string, baseClassName: string): Promise<boolean> {
    try {
        // 调用 writeFile 方法异步写入文件
        await writeFile(fileName, sourceFileContent(className, baseClassName), 'utf8');
        return true;
    } catch (error) {
        console.error('异步创建文件时出错:', error);
    }
    return false;
}

